package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Accident;
import cn.wolfcode.crm.domain.CompensationInfo;
import cn.wolfcode.crm.query.AccidentMapperQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IAccidentService;
import cn.wolfcode.crm.service.ICompensationInfoService;
import cn.wolfcode.crm.service.ILatentClientService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//理赔明细
@Controller
@RequestMapping("accident")
public class AccidentController {

    @Autowired
    private IAccidentService accidentService;

    @Autowired
    private ILatentClientService latentClientService;
    @Autowired
    private ICompensationInfoService iCompensationInfoService;




    @RequestMapping("")
    @PermissionName("理赔明细页面")
    @RequiresPermissions("accident:index")
    public String index(){
        return "accident/list";
    }



    @PermissionName("理赔明细保存")
    @RequiresPermissions("accident:save")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Accident accident){
        JsonResult result=new JsonResult();
        try {
            accidentService.insert(accident);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("备案失败");
        }
        return  result ;
    }


    @PermissionName("理赔明细数据")
    @RequiresPermissions("accident:list")
    @RequestMapping("list")
    @ResponseBody
    private PageResult list(AccidentMapperQueryObject qo){

        return  accidentService.query(qo);
    }

    @PermissionName("理赔明细更新")
    @RequiresPermissions("accident:update")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Accident accident){
        JsonResult result=new JsonResult();
        try {
            accidentService.updateByPrimaryKey(accident);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存成功");
        }
        return  result ;
    }


    @PermissionName("理赔明细更改状态")
    @RequiresPermissions("accident:changeStatus")
    @RequestMapping("changeStatus")
    @ResponseBody
    public JsonResult changeStatus(long id,long status){
        JsonResult result=new JsonResult();
        try {
            accidentService.changeStatus(id,status);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("修改状态失败");
        }
        return  result ;
    }


    @PermissionName("赔偿表保存")
    @RequiresPermissions("accident:saveCompensationInfo")
    @RequestMapping("saveCompensationInfo")
    @ResponseBody
    public JsonResult saveCompensationInfo(CompensationInfo cominfo){
        JsonResult result=new JsonResult();
        try {

            iCompensationInfoService.insert(cominfo);
        }catch (Exception e){
            e.printStackTrace();
            result.markMsg("保存失败");
        }
        return  result ;

    }


    //根据事故id查询赔偿表返回jsonresult
    @RequestMapping("selectcompensationinfoByaccidentId")
    @ResponseBody
    public JsonResult  selectcompensationinfoByaccidentId(Long accidentId){
        JsonResult result=new JsonResult();
        CompensationInfo compensationInfo = iCompensationInfoService.selectByaccidentId(accidentId);
        if(compensationInfo ==null){
            result.markMsg("请编写的你赔偿明细");
            return result;
        }
        return result;
    }

}






